package com.situ.feemanagement.controller;

import com.situ.feemanagement.pojo.Category;
import com.situ.feemanagement.pojo.Medicine;
import com.situ.feemanagement.pojo.query.MedicineQuery;
import com.situ.feemanagement.pojo.vo.InventoryVO;
import com.situ.feemanagement.pojo.vo.MedicineVO;
import com.situ.feemanagement.service.ICategoryService;
import com.situ.feemanagement.service.IMedicineService;
import com.situ.feemanagement.util.PageResult;
import com.situ.feemanagement.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    private IMedicineService medicineService;
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/selectAll")
    public List<Medicine> selectAll() {
        return medicineService.selectAll();
    }

    @RequestMapping("/selectByPage")
    public PageResult<MedicineVO> selectByPage(MedicineQuery medicineQuery) {
        return medicineService.selectByPage(medicineQuery);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(String id) {
        medicineService.deleteById(Integer.parseInt(id));
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(String[] ids) {
        medicineService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(Medicine medicine) {
        medicineService.add(medicine);
        return Result.ok("添加成功");
    }

    @RequestMapping("/selectMap")
    public Result selectMap(String id) {
        Medicine medicine = medicineService.selectById(Integer.parseInt(id));
        List<Category> list = categoryService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("medicine",medicine);
        map.put("category",list);
        return Result.ok(map);
    }

    @RequestMapping("/update")
    public Result update(Medicine medicine) {
        medicineService.update(medicine);
        return Result.ok("更新成功");
    }

    @RequestMapping("/selectQuantity")
    public List<InventoryVO> selectQuantity(String medicineId) {
        return medicineService.selectQuantity(Integer.parseInt(medicineId));
    }

    @RequestMapping("/importExcel")
    public void importExcel(HttpServletRequest request) {
        medicineService.importExcel(request);
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        medicineService.exportExcel(response);
    }
}
